<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="用户ID" prop="userId"
                :rules="[
                 ]">
              <el-input v-model="inputForm.userId" placeholder="请填写用户ID"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签到时间" prop="signInTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.signInTime" placeholder="请填写签到时间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签到类型" prop="signInType"
                :rules="[
                 ]">
              <el-select v-model="inputForm.signInType" placeholder="请选择签到类型">
                <el-option v-for="item in $dictUtils.getDictList('attendance_type')" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签退时间" prop="signOutTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.signOutTime" placeholder="请填写签退时间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签退类型" prop="signOutType"
                :rules="[
                 ]">
              <el-select v-model="inputForm.signOutType" placeholder="请选择签退类型">
                <el-option v-for="item in $dictUtils.getDictList('attendance_type')" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="出勤时间" prop="durationMin"
                :rules="[
                 ]">
              <el-input v-model="inputForm.durationMin" placeholder="请填写出勤时间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签到位置" prop="location"
                :rules="[
                 ]">
              <el-input v-model="inputForm.location" placeholder="请填写签到位置"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="设备信息" prop="deviceInfo"
                :rules="[
                 ]">
              <el-input v-model="inputForm.deviceInfo" placeholder="请填写设备信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          userId: '',
          signInTime: '',
          signInType: '',
          signOutTime: '',
          signOutType: '',
          durationMin: '',
          location: '',
          deviceInfo: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建签到签退`
        } else if (method === 'edit') {
          this.title = '修改签到签退'
        } else if (method === 'view') {
          this.title = '查看签到签退'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/user/attendanceRecord/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.attendanceRecord)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/user/attendanceRecord/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>